// pages/my/index/index.js
import { API_HOST } from "../../../utils/const"
import http from "../../../utils/http"
import util from "../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: API_HOST,
    isLoginStatus: false,
    phoneNumber: '',
    followNum: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    let isLoginStatus = wx.getStorageSync("isLoginStatus") || false;
    let phoneNum = wx.getStorageSync("phoneNum") || '';
    let token = wx.getStorageSync("token") || null;
    this.setData({
      isLoginStatus: isLoginStatus,
      token: token
    })
    var that = this;
    if (isLoginStatus && token) {
      //获取登录手机号码
      that.setData({
        phoneNumber: that.changePhone(phoneNum)
      });
      //获取关注的数量
      that.followNumfn();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  goTel: function (e) {
    let tel = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  /**
    * 没有登录跳转
    */
  isLogin: function () {
    wx.navigateTo({
      url: '../login/login?url=/pages/my/index/index'
    })
  },
  /**
   * 获取关注的数量
   */
  followNumfn: function () {
    var that = this;
    http({
      url: "/attention/count",
      method: "POST",
      success: function (res) {
        console.log("数量", res.data);
        if (res.code == 200) {
          that.setData({
            followNum: res.data
          })
        }
      }
    })
  },
  //退出登录
  outLogin: function () {
    let that = this;
    wx.showToast({
      icon: "none",
      title: "退出登录",
      success: function () {
        wx.removeStorageSync('token')
        wx.removeStorageSync('isLoginStatus')
        wx.removeStorageSync('phoneNum');
        wx.removeStorageSync('userId');
        wx.removeStorageSync('isRealUser');
        that.setData({
          phoneNumber: '',
          isLoginStatus: false,
          followNum: 0
        });
        
      }
    })
  },
  /**
   * 赋值的方法
   */
  setDatafn: function (name, val) {
    this.setData({
      name: val
    });
  },
  /**
  * 改变手机的号码方法
  */
  changePhone: function (num) {
    var str = num + '',
      startNum = '',
      endNum = '',
      changeNum = '',
      newStr = '';
    startNum = str.substring(0, 3);
    endNum = str.substring(7);
    changeNum = str.substring(3, 7).replace(/\d/g, '*');
    return startNum + changeNum + endNum;
  }
})